﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql;
using MySql.Data;
using MySql.Data.MySqlClient;
using MySql.Data.Entity;
using ASV.Classes;
using System.Data;

namespace ASV
{
    public partial class Hinzufügen : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            Authentication auth = new Authentication();
            int userGroup;
            int allowedGroup;

            if (auth.isLoggedIn() == true)
            {
                userGroup = int.Parse(HttpContext.Current.Session["benutzertyp"].ToString());
                allowedGroup = 2;
                if (auth.checkUserGroup(userGroup, allowedGroup) == false)
                {
                    Response.Redirect("~/Error.aspx");
                }
            }
            else
            {
                Response.Redirect("~/Error.aspx");
            }

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Connection mycon = new Connection();
            object[] values = null;
            int numrows;
            int numcells;
            ListItem option;
            DataTable datatable = new DataTable();

            datatable = mycon.select("SELECT ID,Name  FROM `produkttyp`");
            
            for (int x = 0; x < datatable.Rows.Count; x++)
            {
                values = datatable.Rows[x].ItemArray;

                numrows = datatable.Rows.Count;
                numcells = datatable.Columns.Count;

                option = new ListItem();
                option.Value = values[0].ToString();
                option.Text = values[1].ToString();
                prdokttyp.Items.Add(option);
            }
        }

        protected void AddButton_Click(object sender, EventArgs e)
        {
            Connection mycon = new Connection();
            string name = NameID.Text;
            string beschreibung = BeschreibungID.InnerText;
            string seriennummer = SeriennummerID.Text;
            MySqlCommand insertCommand;
            string preis = PreisID.Text;
            string fn;
            string software;
            string SaveLocation;
            string SaveLocationSoftware;

            if ((UrlID1.PostedFile != null) && (UrlID1.PostedFile.ContentLength > 0) && (datei.PostedFile != null) && (datei.PostedFile.ContentLength > 0))
            {
                fn = System.IO.Path.GetFileName(UrlID1.PostedFile.FileName);
                software = System.IO.Path.GetFileName(datei.PostedFile.FileName);
	            SaveLocation = Server.MapPath("/img") + "\\" +  fn;
                SaveLocationSoftware = Server.MapPath("/Software") + "\\" + software;
	            try
	            {
                    datei.PostedFile.SaveAs(SaveLocationSoftware);
                    UrlID1.PostedFile.SaveAs(SaveLocation);
		        }
	            catch ( Exception ex )
	            {
		            Response.Write("Error: " + ex.Message);

	            }
                mycon.insertUpdate("INSERT INTO `produkte` (`Name`, `beschreibung`, `Seriennummer`,`preis`,`bildpfad`,`prodkttype`) VALUES ('" + name + "','" + beschreibung + "','" + seriennummer + "','" + preis + "','" + "img/" + UrlID1.PostedFile.FileName + "','" + prdokttyp.SelectedItem.Value + "')");
                
                mycon.insertUpdate("INSERT INTO `versionsverwaltung` (`ProduktID`, `versionsnummer`,`dateipfad`) VALUES ((SELECT LAST_INSERT_ID()),1,'Software/" + software + "')");
                }
            else
            {
                software = System.IO.Path.GetFileName(datei.PostedFile.FileName);
                SaveLocationSoftware = Server.MapPath("/Software") + "\\" + software;
                try
                {
                    datei.PostedFile.SaveAs(SaveLocationSoftware);
                }
                catch (Exception ex)
                {
                    Response.Write("Error: " + ex.Message);

                }
                mycon.insertUpdate("INSERT INTO `produkte` (`Name`, `beschreibung`, `Seriennummer`,`preis`,`bildpfad`,`prodkttype`) VALUES ('" + name + "','" + beschreibung + "','" + seriennummer + "','" + preis + "','" + "img/140x140.png','" + prdokttyp.SelectedItem.Value + "')");
                
                mycon.insertUpdate("INSERT INTO `versionsverwaltung` (`ProduktID`, `versionsnummer`,`dateipfad`) VALUES ((SELECT LAST_INSERT_ID()),1,'Software/" + software + "')");
            }
            Response.Redirect("~/Admin/Produkte.aspx");
        }
    }
}